package com.apollographql.apollo.response;

import com.apollographql.apollo.api.Error;
import com.apollographql.apollo.api.Operation;
import com.apollographql.apollo.api.Operation.Data;
import com.apollographql.apollo.api.Response;
import com.apollographql.apollo.api.ScalarTypeAdapters;
import com.apollographql.apollo.api.internal.ResolveDelegate;
import com.apollographql.apollo.api.internal.ResponseFieldMapper;
import com.apollographql.apollo.api.internal.Utils;
import com.apollographql.apollo.api.internal.json.ResponseJsonStreamReader;
import com.apollographql.apollo.cache.normalized.internal.ResponseNormalizer;
import com.apollographql.apollo.internal.field.MapFieldValueResolver;
import com.apollographql.apollo.internal.response.RealResponseReader;
import com.apollographql.apollo.subscription.ApolloOperationMessageSerializer;
import com.apollographql.apollo.subscription.OperationServerMessage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class OperationResponseParser<D extends Data, W> {
    final Operation<D, W, ?> operation;
    final ResponseFieldMapper responseFieldMapper;
    final ResponseNormalizer<Map<String, Object>> responseNormalizer;
    final ScalarTypeAdapters scalarTypeAdapters;

    public OperationResponseParser(Operation<D, W, ?> operation2, ResponseFieldMapper responseFieldMapper2, ScalarTypeAdapters scalarTypeAdapters2) {
        this(operation2, responseFieldMapper2, scalarTypeAdapters2, (ResponseNormalizer<Map<String, Object>>) ResponseNormalizer.NO_OP_NORMALIZER);
    }

    public OperationResponseParser(Operation<D, W, ?> operation2, ResponseFieldMapper responseFieldMapper2, ScalarTypeAdapters scalarTypeAdapters2, ResponseNormalizer<Map<String, Object>> responseNormalizer2) {
        this.operation = operation2;
        this.responseFieldMapper = responseFieldMapper2;
        this.scalarTypeAdapters = scalarTypeAdapters2;
        this.responseNormalizer = responseNormalizer2;
    }

    /* JADX DEBUG: Multi-variable search result rejected for r4v1, resolved type: com.apollographql.apollo.api.Operation<D extends com.apollographql.apollo.api.Operation$Data, W, ?> */
    /* JADX WARN: Multi-variable type inference failed */
    public Response<W> parse(Map<String, Object> payload) {
        List<Map<String, Object>> errorPayloads;
        Utils.checkNotNull(payload, "payload == null");
        this.responseNormalizer.willResolveRootQuery(this.operation);
        Data data = null;
        Map<String, Object> buffer = (Map) payload.get(OperationServerMessage.Data.TYPE);
        if (buffer != null) {
            data = (Data) this.responseFieldMapper.map(new RealResponseReader(this.operation.variables(), buffer, new MapFieldValueResolver(), this.scalarTypeAdapters, (ResolveDelegate) this.responseNormalizer));
        }
        List<Error> errors = null;
        if (payload.containsKey("errors") && (errorPayloads = (List) payload.get("errors")) != null) {
            errors = new ArrayList<>();
            for (Map<String, Object> errorPayload : errorPayloads) {
                errors.add(parseError(errorPayload));
            }
        }
        return Response.builder(this.operation).data(this.operation.wrapData((D) data)).errors(errors).dependentKeys(this.responseNormalizer.dependentKeys()).extensions((Map) payload.get(ApolloOperationMessageSerializer.JSON_KEY_EXTENSIONS)).build();
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r6v16, types: [com.apollographql.apollo.api.Operation$Data] */
    /* JADX WARNING: Unknown variable types count: 1 */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public com.apollographql.apollo.api.Response<W> parse(okio.BufferedSource r9) throws IOException {
        /*
        // Method dump skipped, instructions count: 149
        */
        throw new UnsupportedOperationException("Method not decompiled: com.apollographql.apollo.response.OperationResponseParser.parse(okio.BufferedSource):com.apollographql.apollo.api.Response");
    }

    private List<Error> readResponseErrors(ResponseJsonStreamReader reader) throws IOException {
        return reader.nextList(true, new ResponseJsonStreamReader.ListReader<Error>() {
            /* class com.apollographql.apollo.response.OperationResponseParser.AnonymousClass3 */

            @Override // com.apollographql.apollo.api.internal.json.ResponseJsonStreamReader.ListReader
            public Error read(ResponseJsonStreamReader reader) throws IOException {
                return (Error) reader.nextObject(true, new ResponseJsonStreamReader.ObjectReader<Error>() {
                    /* class com.apollographql.apollo.response.OperationResponseParser.AnonymousClass3.AnonymousClass1 */

                    @Override // com.apollographql.apollo.api.internal.json.ResponseJsonStreamReader.ObjectReader
                    public Error read(ResponseJsonStreamReader reader) throws IOException {
                        return OperationResponseParser.parseError(reader.toMap());
                    }
                });
            }
        });
    }

    public static Error parseError(Map<String, Object> payload) {
        String message = "";
        List<Error.Location> locations = new ArrayList<>();
        Map<String, Object> customAttributes = new HashMap<>();
        for (Map.Entry<String, Object> entry : payload.entrySet()) {
            if ("message".equals(entry.getKey())) {
                Object value = entry.getValue();
                message = value != null ? value.toString() : "";
            } else if ("locations".equals(entry.getKey())) {
                List<Map<String, Object>> locationItems = (List) entry.getValue();
                if (locationItems != null) {
                    for (Map<String, Object> item : locationItems) {
                        locations.add(parseErrorLocation(item));
                    }
                }
            } else if (entry.getValue() != null) {
                customAttributes.put(entry.getKey(), entry.getValue());
            }
        }
        return new Error(message, locations, customAttributes);
    }

    private static Error.Location parseErrorLocation(Map<String, Object> data) {
        long line = -1;
        long column = -1;
        if (data != null) {
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                if ("line".equals(entry.getKey())) {
                    line = ((Number) entry.getValue()).longValue();
                } else if ("column".equals(entry.getKey())) {
                    column = ((Number) entry.getValue()).longValue();
                }
            }
        }
        return new Error.Location(line, column);
    }
}
